package module.map.operations.points
{
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.values.CollectionValue;
	
	import common.operations.BaseOperation;
	import common.window.AlertDialog;
	
	import module.map.model.Propertys;
	
	public class RemovePointOperation extends BaseOperation
	{
		private var removeList:Array;
		private var node:CollectionValue;
		private var points:ArrayProperty;
		private var oldState:uint=0;
		public function RemovePointOperation()
		{
			super();
		}
		override public function execute():void
		{
			this.node=this.data.node;
			this.points=this.node.Get(Propertys.POINTS) as ArrayProperty;
			if(this.project.currentMap.selector.selectedList.length==0){
				return;
			}
			oldState=this.project.currentMap.Get(Propertys.STATE).GetValue();
			this.removeList=[];
			var list:Array=this.project.currentMap.selector.selectedList;
			//删除后点集将只剩下不到3个点，非法！
			if(this.points.length-list.length<3){
				AlertDialog.open(this.project.root,"删除所选择点后，点集数量少于3！");
				return;
			}
			var index:int;
			for each (var i:CollectionValue in list) 
			{
				index=this.points.GetChildIndex(i);
				this.removeList.push({index:index,data:i});
			}
			redo();
			this.project.currentMap.recorder.link(this);
		}
		override public function redo():void
		{
			//排序
			this.removeList.sortOn("index",Array.NUMERIC|Array.DESCENDING);
			for each (var i:Object in removeList) 
			{
				this.points.RemoveAt(i.index);
			}
			this.project.currentMap.Update(Propertys.STATE,1);
		}
		override public function undo():void
		{
			this.removeList.sortOn("index",Array.NUMERIC);
			for each (var i:Object in removeList) 
			{
				this.points.AddAt(i.index,i.data);
			}
			this.project.currentMap.Update(Propertys.STATE,this.oldState);
		}
		override public function destroy():void
		{
			this.node=null;
			this.points=null;
			this.removeList.length=0;
			this.removeList=null;
		}
	}
}